package com.yqh;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 字节流的异常处理
 *
 * @author 阳秋豪
 * @createTime 2024/3/7
 */
public class Test3 {
    public static void main(String[] args) throws FileNotFoundException {
        /*---------------- JDK7：IO流中捕获异常的写法(了解即可) ----------------*/
        //    try 后面的小括号中写创建对象的代码，
        //        注意:只有实现了 Autocloseable 接口的类，才能在小括号中创建对象，
        //    try(){
        //
        //    }catch(){
        //
        //    }
        try (FileInputStream fis1 = new FileInputStream("c.txt");
             FileOutputStream fos1 = new FileOutputStream("copy.txt")) {
            int read;
            while ((read = fis1.read()) != -1) {
                fos1.write(read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        /*---------------- JDK9：IO流中捕获异常的写法(了解即可) ----------------*/
        FileInputStream fis1 = new FileInputStream("c.txt");
        FileOutputStream fos1 = new FileOutputStream("copy.txt");
        try (fis1; fos1) {
            int read;
            while ((read = fis1.read()) != -1) {
                fos1.write(read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
